package jdbc;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBC1 {
    public static void main(String[] args) throws SQLException {
        //让用户输入id和name
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入id: ");
        int id = scanner.nextInt();
        System.out.println("请输入name: ");
        String name = scanner.next();
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/lxy?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

       // MysqlDataSource dataSource1 = new MysqlDataSource();
       // dataSource1.setUrl();
        //dataSource1.setUser();
        //dataSource1.setPassword();
      Connection connection = dataSource.getConnection();
        //System.out.println(connection);

        String sql ="insert into student values(1,'张三') ";
        String sql1 ="insert into student values(?,?) ";

        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        int n = statement.executeUpdate();
        statement.close();
        connection.close();


    }

}
